<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('选课')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <button class="btn btn-warning"><a th:href="@{/website/curriplan}" style="text-decoration:none;color:#FFFFFF">开课计划</a></button>
                        </li>
                        <li>
                            <button class="btn btn-success"><a th:href="@{/website/train}" style="text-decoration:none;color:#FFFFFF">培养计划</a></button>
                        </li>
                        <li>
                            <p>选课日期：</p>
                            <input type="text" id="time" th:if="${time!=null}"
                                   th:value="${#dates.format(time.startTime,'yyyy-MM-dd')}+'_'+${#dates.format(time.endTime,'yyyy-MM-dd')}"
                                   disabled="disabled"/>
                            <input type="text" th:if="${time==null}" value="当前不允许选课" disabled="disabled" id="time3">
                        </li>
                        <input type="text" id="time1" th:if="${time!=null}" th:value="${#dates.format(time.startTime,'yyyy-MM-dd')}" hidden>
                        <input type="text" id="time2" th:if="${time!=null}" th:value="${#dates.format(time.endTime,'yyyy-MM-dd')}" hidden>
                        <li>
                            <p>开课名称：</p>
                            <input type="text" name="kaikeName"/>
                        </li>
                        <!--<li>
                            <p>学年：</p>
                            &lt;!&ndash;<input type="text" name="schoolYear"/>&ndash;&gt;
                            <select th:with="terms=${@term.selectTbTermList()}" name="schoolYear">
                                 <option value="">请选择</option>
                                 <option th:each="term : ${terms}" th:value="${term.id}" th:text="${term.schoolYear +'学年  第'+term.term+'学期'+' (' + #dates.format(term.startTime, 'yyyy-MM-dd') + '&#45;&#45;' + #dates.format(term.endTime, 'yyyy-MM-dd')+') '}"></option>
                            </select>
                        </li>-->
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var xuanFlag = [[${@permission.hasPermi('website:curricula:xuan')}]];
    var removeFlag = [[${@permission.hasPermi('website:kaikecheck:remove')}]];
    // var curriculaFlag=[[${@permission.hasPermi('website:curricula:curriCula')}]];
    var quxiaoFlag = [[${@permission.hasPermi('website:curricula:xuanCurri')}]];
    var prefix = ctx + "website/curricula";

    $(function () {
        var options = {
            url: prefix + "/list",
            modalName: "选课",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '序号',
                    visible: true,
                    formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {
                    field: 'kaikeName',
                    title: '开课名称'
                },
                {
                    field: 'courseName',
                    title: '课程名称'
                },
                {
                    field: 'courseTeacher',
                    title: '任课教师'
                },
                {
                    field: 'classWeek',
                    title: '上课周次'
                },
                {
                    field: 'courseTime',
                    title: '上课时间'
                },
                {
                    field: 'courseAddress',
                    title: '上课地点',

                },
                {
                    field: 'stuMaxNumber',
                    title: '课程人数',
                },
                {
                    field: 'number',
                    title: '已选人数',

                },
                {
                    field: 'classScore',
                    title: '学分',

                },
                {
                    field: 'userStates',
                    title: '状态',
                    formatter: function (value, row, index) {
                        if (value == "0") {
                            return value = "未选课";
                        } else if (value == "1") {
                            return value = "已选课";
                        }
                    }

                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.userStates == "0") {
                            actions.push('<a class="btn btn-success btn-xs ' + xuanFlag + '" href="javascript:void(0)" onclick=" xuan(\'' + row.id + '\',\'' + row.stuMaxNumber + '\')"><i class="fa fa-edit"></i>选课</a> ');
                        } else if (row.userStates == "1") {
                            actions.push('<a class="btn btn-success btn-xs ' + quxiaoFlag + '" href="javascript:void(0)" onclick=" quxiaoxuanke(\'' + row.id + '\')"><i class="fa fa-edit"></i>取消选课</a> ');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function xuan(id, stuMaxNumber) {
        //debugger;
        // var data1 = {
        //     id:id,
        //     name:name,
        // };
        $.modal.confirm("确认要选课吗?", function () {
            /*let time1=new Date($("#time1").val());
            let time2=new Date($("#time2").val());*/
            //格式化日期,月日时分秒保持两位
            var time1 = $("#time1").val();
            var time2 = $("#time2").val();
            var date = new Date().Format("yyyy-MM-dd");
            console.log(time1)
            console.log(time2)
            console.log(date)
            debugger;
            if ($("#time3").val() == "当前不允许选课") {
                $.modal.alertError("不在选课时间内");
            } else {
                if (date < time1 || date > time2) {
                    $.modal.alertError("不在选课时间内");
                } else {
                    $.ajax({
                        type: 'post',
                        dataType: 'json',
                        url: prefix + "/xuan",
                        data: {
                            id: id,
                            stuMaxNumber: stuMaxNumber
                        },
                        cache: false,
                        async: true,
                        success: function (data) {
                            $.modal.alertSuccess("选课成功");
                            $.table.refresh();

                        }
                    });
                }
            }
        });

    }

    function quxiaoxuanke(id) {
        //debugger;
        /* var loginName = $.table.selectColumns("loginName");
         alert(loginName);
         return false;*/
        $.modal.confirm("确认要取消选课吗?", function () {
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: prefix + "/xuanCurri",
                data: {
                    id: id,
                },
                cache: false,
                async: true,
                success: function (data) {
                    $.modal.alertSuccess("取消成功");
                    $.table.refresh();
                }
            });
        })
    }

    Date.prototype.Format = function (fmt) { // author: meizz
        var o = {
            "M+": this.getMonth() + 1, // 月份
            "d+": this.getDate(), // 日
            "h+": this.getHours(), // 小时
            "m+": this.getMinutes(), // 分
            "s+": this.getSeconds(), // 秒
            "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
            "S": this.getMilliseconds() // 毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
</script>
</body>
</html>